package fda.schema;

import java.io.File;
import java.io.FileReader;
import java.io.LineNumberReader;
import java.util.LinkedList;
import java.util.List;

import fda.base.Constants;

public class Schema {

	public static final String TABLE = "table";
	public static final String KEY = "key";

	public List<TableInfo> tables = new LinkedList<>();

	public void add(TableInfo table) {
		tables.add(table);
	}

	public List<TableInfo> getTables() {
		return tables;
	}

	public void init(String schemaPath) {

		String line = null;
		File f = new File(schemaPath);
		try {
			LineNumberReader lnr = null;
			try {
				lnr = new LineNumberReader(new FileReader(f));
				TableInfo t = null;
				String[] tokens;

				while (true) {
					line = lnr.readLine();
					if (line == null) {
						break;
					}
					else {
						tokens = parseLine(line);
						if (tokens.length == 2 || tokens.length == 3) {
							if (TABLE.equalsIgnoreCase(tokens[0])) {
								t = new TableInfo(Constants.PRE_FILES + tokens[1], this);
								tables.add(t);
							}
							else {
								int colSize = Integer.parseInt(tokens[1]);
								t.addCol(new Column(tokens[0], colSize));
								if (tokens.length == 3 && KEY.equalsIgnoreCase(tokens[2])) {
									t.setPrimeKeyIndex(t.getCols().size() - 1);
								}
							}
						}
					}
				}

			}
			finally {
				if (lnr != null)
					lnr.close();
			}
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private String[] parseLine(String line) {
		return line.split("\\s+");
	}

	/**
	 * Join information from both tables t1 and t2. Create a new table information instance that include 
	 * all columns from t1 and t2 with shared joined column.
	 * @param t1 table1 to join
	 * @param t2 table2 to join
	 * @return new TableInfo that has all columns from t1 and t2, with shared coumn (key)
	 */
	public static TableInfo joinTables(TableInfo t1, TableInfo t2, String fileName) {
		String joinTableName = null;
		if(fileName == null){
			String t1_name = t1.getDataPath().substring(0, t1.getDataPath().lastIndexOf("."));
			t1_name = t1_name.substring(t1_name.lastIndexOf("/") + 1);
			String t2_name = t2.getDataPath().substring(0, t2.getDataPath().lastIndexOf("."));
			t2_name = t2_name.substring(t2_name.lastIndexOf("/") + 1);
			joinTableName = "sub/join(" + t1_name + "-" + t2_name + ").txt";
		} else {
			joinTableName = fileName;
		}
		
		TableInfo joinTable = new TableInfo(joinTableName, null);

		//add joined column
		String joinCol1 = t1.getCols().get(t1.getPrimeKeyIndex()).name;
		String joinCol2 = t2.getCols().get(t2.getPrimeKeyIndex()).name;
		joinTable.addCol(new Column(joinCol1 + "-" + joinCol2, t2.getCols().get(t2.getPrimeKeyIndex()).length));

		//set first column as a prime key
		joinTable.setPrimeKeyIndex(0);

		//add rest of columns from first table
		for (int i = 0; i < t1.getCols().size(); i++) {
			if (i != t1.getPrimeKeyIndex()) {
				joinTable.addCol(t1.getCols().get(i));
			}
		}

		//add rest of columns from first table
		for (int ii = 0; ii < t2.getCols().size(); ii++) {
			if (ii != t2.getPrimeKeyIndex()) {
				joinTable.addCol(t2.getCols().get(ii));
			}
		}

		return joinTable;
	}

}
